package org.dingyom.gift.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.dingyom.gift.clients.LoginUserClient;
import org.dingyom.gift.clients.SmsClient;
import org.dingyom.gift.domain.LoginUser;
import org.dingyom.gift.domain.User;
import org.dingyom.gift.dto.RegisterDto;
import org.dingyom.gift.dto.SmsDto;
import org.dingyom.gift.exception.asserts.Assert;
import org.dingyom.gift.exception.code.StatusCode;
import org.dingyom.gift.mapper.UserMapper;
import org.dingyom.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.dingyom.gift.util.AjaxResult;
import org.dingyom.gift.util.encrypt.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author dingyom
 * @since 2022-10-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginUserClient loginUserClient;
    @Autowired
    private SmsClient smsClient;
    @Override
    public AjaxResult sendSMS(RegisterDto dto) {
        String tel = dto.getTel();
        //判断手机号是否已经被注册了
        User user = userMapper.loadByPhone(tel);
        Assert.isNull(user,StatusCode.CODE_400_USER_PHONE_EXSIT);
        AjaxResult ajaxResult = smsClient.sendSms(tel);
        if (!ajaxResult.isSuccess()){
            return ajaxResult;
        }

        return AjaxResult.me();
    }

    @Override
    //@Transactional
    @GlobalTransactional//使用seata 时不能用@Transactional 使用这个
    public AjaxResult register(RegisterDto dto) {
        String tel = dto.getTel();
        String password = dto.getPassword();
        String sms = dto.getSms();


        AjaxResult verify = smsClient.verify(new SmsDto(tel, sms));
        if (!verify.isSuccess()){
            return verify;
        }

        //盐值
        String uuid = UUID.randomUUID().toString();
        String salt = "dingyom"+uuid;
        //MD5加密
        String passForMD5 = MD5.getMD5(password+salt);
        //保存loginuser
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername("用户"+tel);
        loginUser.setTel(tel);
        loginUser.setSalt(salt);
        loginUser.setPassword(passForMD5);
        loginUser.setType(0);//1表示管理人员 0表示用户
        AjaxResult ajaxResult = loginUserClient.saveUser(loginUser);
        Object obj = ajaxResult.getResultObj();
        //这里有个坑，Long存放进一个Object的内存中 比如AjaxResult 和map<Sstring,Object>中 再拿出来会降级成为Integer
        Integer loginInteger = (Integer)obj;
        Long loginId = loginInteger.longValue();
        //保存user信息
        User user = new User();
        user.setCreateTime(new Date());//seata的一个bug 会报错 Could not set parameters for mapping: ParameterMapping{property='createTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null',
        user.setPhone(tel);
        user.setPassword(passForMD5);
        user.setSalt(salt);
        user.setBitState(1);
        user.setSecLevel(1);
        user.setLoginId(loginId);
        user.setUsername("用户"+tel);
        userMapper.insert(user);
        return AjaxResult.me();
    }
}
